/**
	AUTHOR		: VEERU
	DATE		: FEB 15th 2004
	DESCRIPTION	: THIS PROCEDURE IS USED TO SAVE OR UPDATE THE PROJECT SETUP DETAILS.
*/


ALTER PROCEDURE SP_SAVE_PROJECT_SETUP

	@PROJNAME   VARCHAR(100),
	@CUSTID     INT,
	@PROJSTATUS VARCHAR(100),
	@OLDPROJID  INT,
	@ODMID      INT,
    @ENDUSERID  INT,
	@SPERSONID  INT,
	@PROJID     INT OUTPUT

AS

BEGIN
	DECLARE @DB_SPERSONID INT
	DECLARE @DB_CUSTID INT
	DECLARE @DB_ODMID INT
    DECLARE @DB_ENDUSERID INT

	IF ( @OLDPROJID < 0 )
	BEGIN
		IF ( ( SELECT COUNT(*) FROM PROJSETUP WHERE PROJNAME = @PROJNAME AND PROJDISABLED = 1 ) > 0 )
		BEGIN
			SET @PROJID = @OLDPROJID
		END
		ELSE
		BEGIN
			IF ( ( SELECT COUNT(*) FROM PROJSETUP ) = 0 )
			BEGIN
				INSERT INTO PROJSETUP( PROJNAME, CUSTID, PROJSTATUS, USERTXT1, USERTXT2 , USERTXT3, USERTXT4, USERTXT5, USERDATE1, USERDATE2, USERLIST1, USERLIST2, USERLIST3, ODMID, USERTXT6, USERTXT7 , USERTXT8, USERTXT9, USERTXT10, USERDATE3, USERDATE4, USERLIST4, USERLIST5, USERLIST6, SPERSONID, ENDUSERID ) VALUES ( @PROJNAME, @CUSTID, @PROJSTATUS, '', '' , '', '', '', '', '', '', '', '', @ODMID, '', '' , '', '', '', '', '', '', '', '', @SPERSONID, @ENDUSERID )
			END
			ELSE
			BEGIN
				INSERT INTO PROJSETUP( PROJNAME, CUSTID, PROJSTATUS, USERTXT1, USERTXT2 , USERTXT3, USERTXT4, USERTXT5, USERDATE1, USERDATE2, USERLIST1, USERLIST2, USERLIST3, ODMID, USERTXT6, USERTXT7 , USERTXT8, USERTXT9, USERTXT10, USERDATE3, USERDATE4, USERLIST4, USERLIST5, USERLIST6, SPERSONID, ENDUSERID ) ( SELECT @PROJNAME, @CUSTID, @PROJSTATUS, USERTXT1, USERTXT2 , USERTXT3, USERTXT4, USERTXT5, USERDATE1, USERDATE2, USERLIST1, USERLIST2, USERLIST3, @ODMID, USERTXT6, USERTXT7 , USERTXT8, USERTXT9, USERTXT10, USERDATE3, USERDATE4, USERLIST4, USERLIST5, USERLIST6, @SPERSONID, @ENDUSERID FROM PROJSETUP WHERE PROJID = ( SELECT MAX(PROJID) FROM PROJSETUP ) )
			END		
			SET @PROJID = ( SELECT @@IDENTITY ) 		
			UPDATE FCUENTRYVAL SET PROJID = @PROJID WHERE TABLENAME = 'PROJSETUP' AND PROJID = @OLDPROJID		
		END
	END
	ELSE
	BEGIN
		SELECT  @DB_SPERSONID = SPERSONID, @DB_CUSTID = CUSTID, @DB_ODMID = ODMID, @DB_ENDUSERID = ENDUSERID FROM PROJSETUP WHERE PROJID = @OLDPROJID
		-- TO UPDATE SALES PERSON NAME, CUSTOMER NAME AND ODM NAME IN FORECAST MASTER TABLE IF THE ARE CHANGED FOR PROJECT
		IF ( @SPERSONID <> @DB_SPERSONID OR @CUSTID <> @DB_CUSTID OR ISNULL( @ODMID, 0 ) <> ISNULL( @DB_ODMID, 0 ) )
		BEGIN
			--INSERTING SALES FORCAST DETAILS FOR THE OLD PART NUMBER INTO FORECAST HISTORY TABLE
			INSERT INTO FCENTRYHISTORY ( ROWID, SPERSONID, PRODID, CUSTID, ODMID, PROJID, MONTH, QUARTER, YEAR, QTY, PRICE,
				             CONFIDENCE_LEVEL, USERLIST1, USERDATE1, USERTXT1,USERTXT2, USERID, ENTRYDATE )
				   	   ( SELECT ROWID, SPERSONID, PRODID, CUSTID, ODMID,PROJID,MONTH, QUARTER, YEAR, QTY, PRICE,
				     	     CONFIDENCE_LEVEL, USERLIST1, USERDATE1,USERTXT1,USERTXT2, USERID, ENTRYDATE 
				FROM FCENTRYMASTER WHERE PROJID = @OLDPROJID )
			--UPDATING SALES FORCAST DETAILS WITH THE NEW PROJECT DETAILS
			UPDATE FCENTRYMASTER SET SPERSONID = @SPERSONID, CUSTID = @CUSTID, ODMID = ISNULL( @ODMID, @CUSTID ) WHERE PROJID = @OLDPROJID
	
			INSERT INTO PROJSETUPHISTORY(PROJID, PROJNAME, CUSTID, PROJSTATUS, USERLIST1, USERLIST2, USERLIST3, USERDATE1, USERDATE2, USERTXT1, USERTXT2, USERTXT3, USERTXT4, USERTXT5, USERLIST1VAL, USERLIST2VAL, USERLIST3VAL, PROJDISABLED, ODMID, USERLIST4, USERLIST5, USERLIST6, USERDATE3, USERDATE4, USERTXT6, USERTXT7, USERTXT8, USERTXT9, USERTXT10, USERLIST4VAL, USERLIST5VAL, USERLIST6VAL, SPERSONID)
					    ( SELECT PROJID, PROJNAME, CUSTID, PROJSTATUS, USERLIST1, USERLIST2, USERLIST3, USERDATE1, USERDATE2, USERTXT1, USERTXT2, USERTXT3, USERTXT4, USERTXT5, USERLIST1VAL, USERLIST2VAL, USERLIST3VAL, PROJDISABLED, ODMID, USERLIST4, USERLIST5, USERLIST6, USERDATE3, USERDATE4, USERTXT6, USERTXT7, USERTXT8, USERTXT9, USERTXT10, USERLIST4VAL, USERLIST5VAL, USERLIST6VAL, SPERSONID FROM PROJSETUP WHERE PROJID = @OLDPROJID ) 
	
		END

		UPDATE PROJSETUP SET PROJNAME = @PROJNAME, CUSTID = @CUSTID, PROJSTATUS = @PROJSTATUS, ODMID = @ODMID, SPERSONID = @SPERSONID, ENDUSERID = @ENDUSERID WHERE PROJID = @OLDPROJID
		SET @PROJID = @OLDPROJID
	END
END

